var RegisterHandler = function (){

}
RegisterHandler.prototype.listenSendCaptchaEvent=function () {
    var callback=function (event) {
        var $this=$(this);
        event.preventDefault();
        var reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/;
        var email=$("input[name='email']").val();
        if(!email || !reg.test(email)){
            alert("邮箱格式不正确！");
            return;
        }
        zlajax.get({
            url:'/email/captcha/?email='+email,
            success:function (result) {
                if(result['code']==200){
                    console.log("邮件发送成功");
                    $this.off("click");
                    $this.attr("disabled","disabled");
                    var countdown=60;
                    var interval=setInterval(function () {
                        if(countdown>0){
                            $this.text(countdown+"s后重新发送");
                        }else{
                            $this.attr("disabled",false);
                            $this.text("发送验证码");
                            $this.on("click",callback);
                            clearInterval(interval);
                        }
                        countdown--;
                    },1000)
                }else{
                    var message=result['message'];
                    alert(message);
                }
            }
        })
    };
    $("#sms-captcha-btn").on("click",callback);
}
RegisterHandler.prototype.listenGraphCaptchaChange=function () {
    $("#captcha-img").on("click",function () {
        var $this=$(this);
        var src=$this.attr("src");
        let new_src=zlparam.setParam(src,"sign",Math.random())
        $this.attr("src",new_src);
    })
}
RegisterHandler.prototype.listenRegisterRequest=function () {
    $("#submit-btn").on("click",function (event) {
        var email=$("input[name='email']").val();
        var email_captcha=$("input[name='email_captcha']").val();
        var username=$("input[name='username']").val();
        console.log(username);
        var password1=$("input[name='password1']").val();
        var password2=$("input[name='password2']").val();
        var graph_captcha=$("input[name='graph_captcha']").val();
        event.preventDefault();
        zlajax.post({
            url: '/register/',
            data:{
                'email':email,
                'email_captcha':email_captcha,
                'username':username,
                'password1':password1,
                'password2':password2,
                "graph_captcha":graph_captcha
            },
            success:function (result) {
                if(result['code']==200){
                    console.log('注册成功!');
                    window.location = "/login/";
                }else{
                    console.log(result['message'])
                }
            }
        })
    })
}
RegisterHandler.prototype.run=function () {
    this.listenSendCaptchaEvent();
    this.listenGraphCaptchaChange();
    this.listenRegisterRequest();
}
$(function () {
    var handler=new RegisterHandler();
    handler.run();
})